package com.jingdianjichi.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;


import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @author zwy
 * @version 1.0
 * @description: 数据库访问加密工具类
 * @date 2025/7/5 11:25
 */
public class DruidEncryptUtil {
    /**
     * 私钥 - 用于加密操作
     */
    public static String privateKey = "MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAqxmxNwOc9IoS6MVVZ3ZnWpiP2agHcObaeUR+8jVyWMnivn21teh/+QvbPrDeEaY3i5DdarM0egK4i++Rp2HgawIDAQABAkAK1JXnhsg5B0LQwfaSYmfpNisBrgv6iFv5ie2dmBoXpv7xdVK3TmxssBgbyWSvY3SaR9K3/Fqn7eeO+5QFFx3xAiEAzc94Eks5FWGOUwHqJ54cVQAwiwGbkN1hFiy0fwVGJi0CIQDU009VrAcvdC9CPYXWkQ1g2F1fSGTDKlu190PGGHkX9wIgObHjcx1rTzcd8t8iiSClyJ5Y/V7iAWZOBS1bHBCabbECIDDx5+zsAzsGnVe+jmkqMsly+QZQv9uigjT3CL8mIbNBAiBeuqLo+94je9ZXKxPNQeFXI1xtfUhoHmv/XfgXvZjSnA==";


    /**
     * 公钥 - 用于解密操作
     */
    public static String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKsZsTcDnPSKEujFVWd2Z1qYj9moB3Dm2nlEfvI1cljJ4r59tbXof/kL2z6w3hGmN4uQ3WqzNHoCuIvvkadh4GsCAwEAAQ==";



    /**
     * 使用私钥对明文进行加密
     *
     * @param plainText 明文内容
     * @return 加密后的字符串
     * @throws Exception 加密过程中的异常
     */
    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt:" + encrypt);
        return encrypt;
    }

    /**
     * 使用公钥对密文进行解密
     *
     * @param encryptText 加密后的字符串
     * @return 解密后的明文
     * @throws Exception 解密过程中的异常
     */
    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey, encryptText);
        System.out.println("decrypt:" + decrypt);
        return decrypt;
    }

    /**
     * 主方法 - 测试加密解密功能
     *
     * @param args 命令行参数（未使用）
     * @throws Exception 测试过程中可能抛出的异常
     */
    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("123456");
        System.out.println("encrypt:" + encrypt);
        decrypt(encrypt);
    }
}